package com.cgbl.cg.vm.home

import android.app.Application
import androidx.databinding.ObservableField
import androidx.databinding.ObservableInt
import com.cgbl.cg.vm.main.HomeFragmentViewModel
import com.robot.base.base.vm.GLBaseViewModel
import me.goldze.mvvmhabit.binding.command.BindingAction
import me.goldze.mvvmhabit.binding.command.BindingCommand
import me.goldze.mvvmhabit.bus.event.SingleLiveEvent

class SearchViewModel(application: Application) : GLBaseViewModel(application) {
    val type = ObservableField(true)

    val searchList = arrayListOf<String>()
    val hotSearchList = arrayListOf<String>()
    val tabType = ObservableInt(HomeFragmentViewModel.TAB_RECOMMEND)
    val uc = UiChangeEvent()
    val searchContent = ObservableField<String>()
    val leftFilter = ObservableField<String>("综合排序")
    val rightFilter = ObservableField<String>("时间不限")
    val leftList = arrayListOf<String>()
    val rightList = arrayListOf<String>()

    class UiChangeEvent {
        val tabType: SingleLiveEvent<Int> = SingleLiveEvent()
        val searchDataEvent = SingleLiveEvent<Any>()
        val filterEvent = SingleLiveEvent<Boolean>()
    }

    val backCommand = BindingCommand<Any>(BindingAction {
        type.set(true)
    })

    val clickSelectAll = BindingCommand<Any>(BindingAction {
        tabType.set(TAB_ALL)
        uc.tabType.postValue(TAB_ALL)
    })


    val clickSelectWork = BindingCommand<Any>(BindingAction {
        tabType.set(TAB_WORK)
        uc.tabType.postValue(TAB_WORK)
    })

    val clickSelectCourse = BindingCommand<Any>(BindingAction {
        tabType.set(TAB_COURSE)
        uc.tabType.postValue(TAB_COURSE)
    })

    val clickSelectResource = BindingCommand<Any>(BindingAction {
        tabType.set(TAB_RESOURCE)
        uc.tabType.postValue(TAB_RESOURCE)
    })

    val clickSelectProject = BindingCommand<Any>(BindingAction {
        tabType.set(TAB_PROJECT)
        uc.tabType.postValue(TAB_PROJECT)
    })

    val clickSelectUser = BindingCommand<Any>(BindingAction {
        tabType.set(TAB_USER)
        uc.tabType.postValue(TAB_USER)
    })

    val clickLeftFilterCommand = BindingCommand<Any>(BindingAction {
        uc.filterEvent.postValue(true)
    })

    val clickRightFilterCommand = BindingCommand<Any>(BindingAction {
        uc.filterEvent.postValue(false)
    })

    fun initData() {
        leftList.clear()
        rightList.clear()
        leftList.addAll(arrayListOf("综合排序", "最热优先", "最新优先"))
        rightList.addAll(arrayListOf("时间不限", "最新一天", "最近一周", "最近三个月"))

    }

    fun search() {
        type.set(false)
        searchList.add(searchContent.get() ?: "")
        hotSearchList.add(searchContent.get() ?: "")
        uc.searchDataEvent.call()
        initData()
    }

    companion object {
        const val TAB_ALL = 0

        const val TAB_WORK = 1

        const val TAB_COURSE = 2

        const val TAB_RESOURCE = 3

        const val TAB_PROJECT = 4

        const val TAB_USER = 5
    }
}